package com.esri.core.internal.io.handler;

import com.esri.core.io.EsriSecurityException;
import com.esri.core.io.UserCredentials;
import com.esri.core.portal.Portal;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/* loaded from: classes.dex */
public class b extends UserCredentials {
    private static final long serialVersionUID = -2557709773935397308L;
    Portal a;
    private String b;
    private String c;
    private UserCredentials d;

    public b(String str, Portal portal, String str2) throws EsriSecurityException {
        this(str, portal, str2, true);
    }

    public b(String str, Portal portal, String str2, boolean z) throws EsriSecurityException {
        this.a = portal;
        this.b = str;
        this.d = portal.getCredentials();
        this.c = str2;
        setAuthenticationType(UserCredentials.AuthenticationType.TOKEN);
        UserCredentials userCredentials = this.d;
        if (userCredentials != null && userCredentials.getUserName() != null && this.d.getUserName().trim().length() > 0 && this.d.getPassword() != null && this.d.getPassword().length() > 0) {
            setUserAccount(this.d.getUserName(), this.d.getPassword());
        }
        UserCredentials userCredentials2 = this.d;
        if (userCredentials2 != null) {
            if (userCredentials2 instanceof k) {
                k kVar = (k) userCredentials2;
                if (kVar.e() != null) {
                    setTokenServiceUrl(kVar.e() + "/generateToken");
                }
            } else if (userCredentials2.getTokenServiceUrl() != null) {
                setTokenServiceUrl(this.d.getTokenServiceUrl());
            }
        }
        if (z) {
            e();
        }
        if (this.d != null) {
            if (getUserName() == null || getUserName().trim().length() == 0) {
                setUserName(this.d.getUserName());
            }
        }
    }

    public b(b bVar) {
        super(bVar);
        if (bVar != null) {
            this.b = bVar.b;
            this.c = bVar.c;
            this.d = bVar.d;
            this.a = bVar.a;
        }
    }

    public UserCredentials a() {
        return this.d;
    }

    public String b() {
        return this.c;
    }

    @Override // com.esri.core.io.UserCredentials
    public UserCredentials getCopy() {
        return new b(this);
    }

    private void d() throws EsriSecurityException {
        c.a(n.a(this.a.getUrl(), this.d), 1);
    }

    private String e() throws EsriSecurityException {
        n a = n.a(this.b, getCopy());
        if (a != null) {
            c.a(a, 2);
            UserCredentials userCredentials = a.b;
            String token = userCredentials.getToken();
            setUserToken(token, userCredentials.getTokenExpiry(), userCredentials.getReferer());
            setTokenServiceUrl(userCredentials.getTokenServiceUrl());
            return token;
        }
        return null;
    }

    @Override // com.esri.core.io.UserCredentials
    public int hashCode() {
        String str = this.b;
        int hashCode = ((str == null ? 0 : str.hashCode()) + 31) * 31;
        String str2 = this.c;
        int hashCode2 = (hashCode + (str2 == null ? 0 : str2.hashCode())) * 31;
        UserCredentials userCredentials = this.d;
        return ((hashCode2 + (userCredentials == null ? 0 : userCredentials.hashCode())) * 31) + (this.a.getUrl() != null ? this.a.getUrl().hashCode() : 0);
    }

    @Override // com.esri.core.io.UserCredentials
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj != null && getClass() == obj.getClass()) {
            b bVar = (b) obj;
            String str = this.b;
            if (str == null) {
                if (bVar.b != null) {
                    return false;
                }
            } else if (!str.equals(bVar.b)) {
                return false;
            }
            String str2 = this.c;
            if (str2 == null) {
                if (bVar.c != null) {
                    return false;
                }
            } else if (!str2.equals(bVar.c)) {
                return false;
            }
            UserCredentials userCredentials = this.d;
            if (userCredentials == null) {
                if (bVar.d != null) {
                    return false;
                }
            } else if (!userCredentials.equals(bVar.d)) {
                return false;
            }
            if (this.a.getUrl() == null) {
                if (bVar.a.getUrl() != null) {
                    return false;
                }
            } else if (!this.a.getUrl().equals(bVar.a.getUrl())) {
                return false;
            }
            return true;
        }
        return false;
    }

    public synchronized String c() throws EsriSecurityException {
        String str;
        str = null;
        if (System.currentTimeMillis() >= getTokenExpiry()) {
            d();
            str = e();
        }
        return str;
    }

    private void writeObject(ObjectOutputStream objectOutputStream) throws IOException {
        objectOutputStream.writeObject(this.b);
        objectOutputStream.writeObject(this.c);
        objectOutputStream.writeObject(this.d);
        objectOutputStream.writeObject(this.a.getUrl());
    }

    private void readObject(ObjectInputStream objectInputStream) throws ClassNotFoundException, IOException {
        if (UserCredentials.isDefaultReadObject()) {
            objectInputStream.defaultReadObject();
            return;
        }
        this.b = (String) objectInputStream.readObject();
        this.c = (String) objectInputStream.readObject();
        this.d = (UserCredentials) objectInputStream.readObject();
        this.a = new Portal((String) objectInputStream.readObject(), this.d);
    }
}
